package com.example.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ResultCode;
import com.example.dao.*;
import com.example.entity.Xitongjianjie;
import com.example.exception.CustomException;
import com.example.service.XitongjianjieService;
import com.example.vo.XitongjianjieVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

@Service("xitongjianjieService")
public class XitongjianjieServiceImpl extends ServiceImpl<XitongjianjieDao, Xitongjianjie> implements XitongjianjieService {

    @Resource
    private XitongjianjieDao xitongjianjieDao;

    public Xitongjianjie add(Xitongjianjie xitongjianjie) {
		
	    LambdaQueryWrapper<Xitongjianjie> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Xitongjianjie::getLeibie,xitongjianjie.getLeibie());
        Xitongjianjie findXitongjianjie = xitongjianjieDao.selectOne(wrapper);
        if (findXitongjianjie != null) {
            throw new CustomException("1001", "" + xitongjianjie.getLeibie() + "\"已存在");
        }
        xitongjianjie.setAddtime(new Date());
        xitongjianjieDao.insert(xitongjianjie);
        return xitongjianjie;
    }
	
	
	
    public void delete(Long id) {
        xitongjianjieDao.deleteById(id);
    }
	

    public void update(Xitongjianjie xitongjianjie) {
         LambdaQueryWrapper<Xitongjianjie> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Xitongjianjie::getLeibie,xitongjianjie.getLeibie());
        Xitongjianjie findXitongjianjie = xitongjianjieDao.selectOne(wrapper);
		if (findXitongjianjie != null && xitongjianjie.getId() != findXitongjianjie.getId()) {
            throw new CustomException("1001", "" + xitongjianjie.getLeibie() + "\"已存在");
        }
        xitongjianjieDao.updateById(xitongjianjie);
    }
	
	@Override
    public List<Map<String, Object>> daochuexcel() {
        return xitongjianjieDao.daochuexcel();
    }

    @Override
    public Xitongjianjie findById(Integer id) {
        return xitongjianjieDao.selectById(id);
    }

    @Override
    public Xitongjianjie findByZhujian(String lb) {
        LambdaQueryWrapper<Xitongjianjie> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Xitongjianjie::getLeibie,lb);
        return xitongjianjieDao.selectOne(wrapper);
    }



    public XitongjianjieVo findPage(XitongjianjieVo xitongjianjieVo) {
        LambdaQueryWrapper<Xitongjianjie> wrapper = Wrappers.lambdaQuery();
		wrapper.like(ObjectUtils.isNotEmpty(xitongjianjieVo.getLeibie()), Xitongjianjie::getLeibie,xitongjianjieVo.getLeibie());
        wrapper.like(ObjectUtils.isNotEmpty(xitongjianjieVo.getNeirong()), Xitongjianjie::getNeirong,xitongjianjieVo.getNeirong());
        
		wrapper.orderByDesc(Xitongjianjie::getId);
        Page<Xitongjianjie> page = new Page<>(xitongjianjieVo.getCurrent(),xitongjianjieVo.getCurrentNum());
        xitongjianjieDao.selectPage(page,wrapper);
        List<Xitongjianjie> xitongjianjies = page.getRecords();
        xitongjianjieVo.setList(xitongjianjies);
        xitongjianjieVo.setPages(page.getPages());
        xitongjianjieVo.setTotal(page.getTotal());
        xitongjianjieVo.setCurrent(page.getCurrent());
        xitongjianjieVo.setCurrentNum(page.getSize());
        return xitongjianjieVo;
    }

    @Override
    public void deleteList(List<Xitongjianjie> list) {
        List<Integer> ids =  list.stream().map(Xitongjianjie::getId).collect(Collectors.toList());
        xitongjianjieDao.deleteBatchIds(ids);
    }
	
	
	
	
	
	//youxxianxci
}
